package com.demo.common;

import java.util.Properties;

import com.demo.controller.CommonNewController;
import com.jfinal.ext.plugin.quartz.QuartzPlugin;
import org.apache.log4j.Logger;



import com.demo.controller.CommonController;
import com.demo.index.IndexController;
import com.demo.mb.MbController;
import com.demo.mb.WxMb;
import com.demo.mb.WxMbsx;
import com.demo.menu.WxMenu;
import com.demo.menu.WxMenuController;
import com.demo.msg.MsgTypeController;
import com.demo.msg.Msgtype;
import com.demo.msg.WxMessage;
import com.demo.msg.WxMessageController;
import com.demo.util.PropertiesHelper;
import com.demo.util.UploadController;
import com.demo.weixin.DemosController;
import com.demo.weixin.WxContent;
import com.demo.weixin.WxContentController;
import com.demo.index.AuthInterceptor;
import com.demo.index.WxInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.demo.controller.Car;

/**
 * API引导式配置
 */
public class DemoConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("a_little_config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", true));
		 // 加载微信配置
        Properties r = PropertiesHelper.loadProperty("/weixin.properties");
        if(!r.isEmpty()){
            try{
            	ApiConfig.setDevMode(r.getProperty("wxdevMode").equals("false"));  //调试模式 false 关闭
                ApiConfig.setUrl(r.getProperty("wxURL"));
                ApiConfig.setToken(r.getProperty("Token"));
                ApiConfig.setAppId(r.getProperty("AppId"));
                ApiConfig.setAppSecret(r.getProperty("AppSecret"));
            }catch(Exception e){
                Logger.getLogger(DemoConfig.class).error("无法读取参数/weixin.properties",e);
            }
        }else{
            Logger.getLogger(DemoConfig.class).error("无法读取 weixin.properties");
        }
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);	// 第三个参数为该Controller的视图存放路径
		me.add("/wxMenu", WxMenuController.class);			
        me.add("/weixin", DemosController.class);
        me.add("/msgType", MsgTypeController.class);
        me.add("/wxMessage", WxMessageController.class);
        me.add("/wxContent", WxContentController.class);
        // 调用haoserver的违章查询接口
   //     me.add("/wzcx", CommonController.class);
     //   me.add("/wzcx", CommonController.class);
        me.add("/wzcx", CommonNewController.class);
        // 调用汽车之家的违章查询接口
    //    me.add("/wzcx", CarFamilyController.class);
        me.add("/mb", MbController.class);
        me.add("/upload", UploadController.class);

	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		 //缓存插件
        me.add(new EhCachePlugin());

        //配置数据库连接池插件

        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbc.url"), getProperty("jdbc.username"), getProperty("jdbc.password").trim());
				druidPlugin.setDriverClass(getProperty("jdbc.driverClass"));
        druidPlugin.setMaxActive(getPropertyToInt("jdbc.maxActive"));
        druidPlugin.setMinIdle(getPropertyToInt("jdbc.minIdle"));
        druidPlugin.setInitialSize(getPropertyToInt("jdbc.initialSize"));
        druidPlugin.setMaxWait(getPropertyToInt("jdbc.maxWait"));
        druidPlugin.setTimeBetweenEvictionRunsMillis(getPropertyToInt("jdbc.timeBetweenEvictionRunsMillis"));
        druidPlugin.setMinEvictableIdleTimeMillis(getPropertyToInt("jdbc.minEvictableIdleTimeMillis"));
        druidPlugin.setValidationQuery(getProperty("jdbc.validationQuery"));
        druidPlugin.setTestWhileIdle(getPropertyToBoolean("jdbc.testWhileIdle"));
        druidPlugin.setTestOnBorrow(getPropertyToBoolean("jdbc.testOnBorrow"));
        druidPlugin.setTestOnReturn(getPropertyToBoolean("jdbc.testOnReturn"));
                 
 
    	 //监控统计 防SQL注入："stat,wall"

        //定时任务调度插件
        QuartzPlugin  quartzPlugin =  new QuartzPlugin("job.properties");
        me.add(quartzPlugin);

        //监控统计 防SQL注入："stat,wall"
    	 

        druidPlugin.setFilters("stat,wall");
        me.add(druidPlugin);
		
				// 配置ActiveRecord插件
				ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
				me.add(arp);
        arp.setShowSql(true);
        arp.addMapping("wx_menu", WxMenu.class);	// 映射wx_menu 表到 Blog模型
				arp.addMapping("wx_msgtype",Msgtype.class);	// 映射wx_msgtype 表到 Blog模型
				arp.addMapping("wx_message", WxMessage.class);	// 映射wx_message 表到 Blog模型
				arp.addMapping("wx_content", WxContent.class);	// 映射wx_content 表到 WxContent模型
		        arp.addMapping("t_car", Car.class);
		        arp.addMapping("wx_mbsx", WxMbsx.class);
		        arp.addMapping("wx_mb", WxMb.class);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
	    me.add(new WxInterceptor());
	    me.add(new AuthInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("ctx"));
        DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
        me.add(dvh);
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}
}
